package com.fwtai.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.datasource.DaoHandle;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 合伙人访问数据库
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2021-08-02 15:26:31
 * @QQ号码 444141300
 * @Email service@yinlz.com
 * @官网 <url>http://www.fwtai.com</url>
 */
@Repository
public class PartnerDao{

  @Resource
  private DaoHandle dao;

  public Long queryExistById(final Long kid){
    return dao.queryForLong("partner.queryExistById",kid);
  }

  //审核
  @Transactional
  public int edit(final PageFormData formData,final Long roleId){
    formData.put("roleId",roleId);
    dao.execute("partner.delPartnerRole",formData);
    return dao.execute("partner.updateStatus",formData);
  }

  //审核
  @Transactional
  public int editOk(final PageFormData formData,final boolean exist,final Long roleId){
    final int rows = dao.execute("partner.updateStatus",formData);
    if(exist){
      dao.execute("partner.updateInvitationCode",formData);
    }
    formData.put("roleId",roleId);
    dao.execute("partner.addPartnerRole",formData);//可重复插入
    return rows;
  }

  @Transactional
  public int editEmptyPartner(final Long kid,final PageFormData formData,final Long roleId){
    dao.execute("partner.updateStatus",formData);
    formData.put("roleId",roleId);
    dao.execute("partner.addPartnerRole",formData);//可重复插入
    return dao.execute("partner.editEmptyPartner",kid);
  }

  @Transactional
  public int editEmptyPartner(final PageFormData formData,final long kid,final Long roleId){
    dao.execute("partner.editEmptyPartner",kid);
    formData.put("roleId",roleId);
    dao.execute("partner.addPartnerRole",formData);//可重复插入
    return dao.execute("partner.updateStatus",formData);
  }

  public HashMap<String, Object> queryById(final String kid){
    return dao.queryForHashMap("partner.queryById",kid);
  }

  public HashMap<String,Object> listData(final PageFormData formData){
    return dao.queryForPage(formData,"partner.listData","partner.listTotal");
  }

  public int editProtocol(final PageFormData formData){
    return dao.execute("partner.editProtocol",formData);
  }

  public String getParentReferrer(final Long userId){
    return dao.queryForString("partner.getParentReferrer",userId);
  }

  public boolean getTypeProxy(final Long provinceId){
    return dao.queryForLong("partner.getTypeProxyProvince",provinceId) != null;
  }

  public boolean getTypeProxy(final Long provinceId,final Long cityId){
    final HashMap<String,Object> params = new HashMap<>(2);
    params.put("provinceId",provinceId);
    params.put("cityId",cityId);
    return dao.queryForLong("partner.getTypeProxyCity",params) != null;
  }

  public boolean getTypeProxy(final Long provinceId,final Long cityId,final Long countyId){
    final HashMap<String,Object> params = new HashMap<>(3);
    params.put("provinceId",provinceId);
    params.put("cityId",cityId);
    params.put("countyId",countyId);
    return dao.queryForLong("partner.getTypeProxyCounty",params) != null;
  }

  public Long getExistProxy(final Long userId,final int type,final Long... area){
    final PageFormData formData = new PageFormData();
    formData.put("userId",userId);
    switch (type){
      case 1:
        formData.put("provinceId",area[0]);
        return dao.queryForLong("partner.getProxyOne",formData);
      case 2:
        formData.put("provinceId",area[0]);
        formData.put("cityId",area[1]);
        return dao.queryForLong("partner.getProxyTwo",formData);
      case 3:
        formData.put("provinceId",area[0]);
        formData.put("cityId",area[1]);
        formData.put("countyId",area[2]);
        return dao.queryForLong("partner.getProxyThree",formData);
      default:
        break;
    }
    return null;
  }

  public List<Integer> getProxySelf(final Long userId){
    return dao.queryListEntity("partner.getProxySelf",userId);
  }

  public String getPartnerCode(final Long kid){
    return dao.queryForString("partner.getPartnerCode",kid);
  }

  public String getStoreCode(final Long kid){
    return dao.queryForString("partner.getStoreCode",kid);
  }

  public HashMap<String, Object> getPartnerReferrer(final Long kid){
    return dao.queryForHashMap("partner.getPartnerReferrer",kid);
  }

  public Integer getReferrerAndVip(final long kid){
    return dao.queryForInteger("partner.getReferrerAndVip",kid);
  }

  public Long getAuditExist(final long areaId){
    return dao.queryForLong("partner.getAuditExist",areaId);
  }

  public List<HashMap<String,Object>> getUserType(final String word){
    return dao.queryForListHashMap("partner.getUserType",word);
  }

  public int addUserInviter(final PageFormData formData){
    return dao.execute("partner.addUserInviter",formData);
  }

  public int editUserInviter(final Long kid){
    return dao.execute("partner.editUserInviter",kid);
  }

  public int addUserReferrer(final PageFormData formData){
    return dao.execute("partner.addUserReferrer",formData);
  }

  public List<HashMap<String,Object>> getPartnerProxy(final PageFormData formData){
    return dao.queryForListHashMap("partner.getPartnerProxy",formData);
  }

  public String getShareParent(final Long userId){
    return dao.queryForString("partner.getShareParent",userId);
  }
}